//
//  SY_PaymentDetailViewController.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 2016/12/29.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_PaymentDetailViewController.h"
#import "SDPhotoBrowser.h"
#import "SY_PaymentWayViewController.h"

@interface SY_PaymentDetailViewController ()<SDPhotoBrowserDelegate>
{
    NSArray *picArr;
    NSInteger lasttype;
    EmptyStateView *emptyView;//空状态
}

@property (nonatomic, strong) NSDictionary *detailDic;

@property (nonatomic, strong) UIScrollView *myScrollView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *fromLabel;
@property (nonatomic, strong) UILabel *classnameLabel;
@property (nonatomic, strong) UILabel *studentnameLabel;
@property (nonatomic, strong) UILabel *orderCodeLabel;
@property (nonatomic, strong) UILabel *payCodeLabel;
@property (nonatomic, strong) UILabel *payTimeLabel;
@property (nonatomic, strong) UIButton *payBtn;

@end

@implementation SY_PaymentDetailViewController

- (void)loadView {
    
    [super loadView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    lasttype = [SYAccountTool ShareAccount].account.lasttype;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.detailModel) {
        [self crearView];
    }else {
        [self loadPaymentDetail];
    }
    
    emptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake((SCREENWIDTH-120)/2, (SCREENHEIGH-110)/2-70, 120, 110) andBackImage:@"list_empty" andBottomStr:@"页面不存在"];
    [self.view addSubview:emptyView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Request

- (void)loadPaymentDetail {
    
    NSMutableDictionary *parmaDic = [[NSMutableDictionary alloc]init];
    
    [parmaDic setObject:@"api_receiptinfo_toparent" forKey:@"method"];
    if (lasttype == 1) {
        [parmaDic setObject:@"0" forKey:@"userid"];
    }else {
        [parmaDic setObject:USERID forKey:@"userid"];
    }
    
    [parmaDic setObject:self.payid forKey:@"payid"];
    
    [Utils get:nil parameters:parmaDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *dataArr = [jsonObject objectForKey:@"re_data"];
        
        if (dataArr != nil && dataArr.count > 0) {
            [self->emptyView hideEmptyImage];
            
            self.detailDic = [[NSDictionary alloc]init];
            self.detailDic = [dataArr objectAtIndex:0];
            self.detailModel = [SY_Par_PaymentModel mj_objectWithKeyValues:self.detailDic];
            
            [self crearView];
            
        }else {
            [self->emptyView showEmptyImage];
        }
        
    } failure:^(NSInteger recode, NSError *error) {
        [self->emptyView showEmptyImage];
    }];
    
}

#pragma mark - Method 

- (void)crearView {
    
    [self.view addSubview:self.myScrollView];
    
    UIImageView *iconImg = [[UIImageView alloc]initWithFrame:CGRectMake(15, 16, 50, 50)];
    [iconImg setImage:[UIImage imageNamed:@"pay_content_icon_"]];
    
    [self.myScrollView addSubview:iconImg];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconImg.right + 12, 20, SCREENWIDTH - 100, 16)];
    [self.titleLabel setTextColor:colorFromHex(@"#3b3b3b")];
    [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
    self.titleLabel.text = self.detailModel.name;
    [self.myScrollView addSubview:self.titleLabel];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(self.titleLabel.x, self.titleLabel.bottom+11, 42, 15)];
    label1.text = @"金额:";
    [label1 setFont:[UIFont systemFontOfSize:15]];
    label1.textColor = colorFromHex(@"#3b3b3b");
    [self.myScrollView addSubview:label1];
    
    self.moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(label1.right, label1.y, SCREENWIDTH - 150, 16)];
    [self.moneyLabel setTextColor:colorFromHex(@"#ff8004")];
    [self.moneyLabel setFont:[UIFont systemFontOfSize:16]];
    
    float money = self.detailModel.money/100.0;
    NSString *moneyStr = [NSString stringWithFormat:@"%.2f元",money];
    NSMutableAttributedString *numAttstr = [[NSMutableAttributedString alloc]initWithString:moneyStr];
    
    
    [numAttstr addAttribute:NSFontAttributeName
                      value:[UIFont systemFontOfSize:15]
                      range:NSMakeRange(moneyStr.length - 1, 1)];
    
    self.moneyLabel.attributedText = numAttstr;
    [self.myScrollView addSubview:self.moneyLabel];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(15, iconImg.bottom + 16, SCREENWIDTH, 0.5)];
    [lineView setBackgroundColor:colorFromHex(@"#DEDEDE")];
    [self.myScrollView addSubview:lineView];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(15, lineView.bottom + 16, 65, 14)];
    label2.text = lasttype == 1?@"收费日期:":@"交费日期:";
    [label2 setFont:[UIFont systemFontOfSize:14]];
    [label2 setTextColor:colorFromHex(@"#979797")];
    [self.myScrollView addSubview:label2];
    
    self.dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(label2.right+2 , label2.y, SCREENWIDTH - 120, 14)];
    [self.dateLabel setTextColor:colorFromHex(@"#3b3b3b")];
    [self.dateLabel setFont:[UIFont systemFontOfSize:14]];
    
    NSString *starDate = [self.detailModel.starttime substringToIndex:10];
    NSString *endDate = [self.detailModel.endtime substringToIndex:10];
    self.dateLabel.text = [NSString stringWithFormat:@"%@至%@",starDate,endDate];
    
    [self.myScrollView addSubview:self.dateLabel];
    
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(15, label2.bottom + 6, 65, 14)];
    label3.text = lasttype == 1?@"收费说明:":@"交费说明:";
    [label3 setFont:[UIFont systemFontOfSize:14]];
    [label3 setTextColor:colorFromHex(@"#979797")];
    [self.myScrollView addSubview:label3];
    
    float contentHeight = [Utils resizeContentLabelHeightWithString:self.detailModel.scontent andLineSpacing:4 andFont:[UIFont systemFontOfSize:14] andMaxWidth:SCREENWIDTH - 120];
    
    self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(label2.right+2 , label3.y-4, SCREENWIDTH - 120, contentHeight)];
    self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    self.contentLabel.numberOfLines = 0;
    [self.contentLabel setTextColor:colorFromHex(@"#3b3b3b")];
    [self.contentLabel setFont:[UIFont systemFontOfSize:14]];
    [self.contentLabel setText:self.detailModel.scontent];
    [self.myScrollView addSubview:self.contentLabel];
    
    if (self.detailModel.pic.length > 0) {
        picArr = [self.detailModel.pic componentsSeparatedByString:@","];
    }
    for (int i = 0; i < picArr.count; i++) {
        
        UIImageView *picImg = [[UIImageView alloc]initWithFrame:CGRectMake(85 + (85 + 10) * (i %3), self.contentLabel.bottom + 16 + (85 + 10) * (i / 3), 85, 85)];
        picImg.userInteractionEnabled = YES;
        
        NSString * picURL = [NSString stringWithFormat:@"%@%@",IMAGEHOSTURL,[picArr objectAtIndex:i]];
        [picImg sd_setImageWithURL:[NSURL URLWithString:picURL] placeholderImage:[UIImage imageNamed:@"em_empty_photo"]];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        
        [picImg addGestureRecognizer:tap];
        picImg.tag = i;
        
        picImg.contentMode = UIViewContentModeScaleAspectFill;
        picImg.clipsToBounds = YES;
        
        [self.myScrollView addSubview:picImg];
    }
    
    float picHeight = picArr.count > 0?ceilf(picArr.count / 3.0) * 95 + 11:0;
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, self.contentLabel.bottom + picHeight + 11, SCREENWIDTH, 10)];
    [view1 setBackgroundColor:colorFromHex(@"#F5F8FA")];
    [self.myScrollView addSubview:view1];
    
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(15, view1.bottom + 16, 60, 14)];
    [label4 setText:@"发起者:"];
    [label4 setTextColor:colorFromHex(@"#979797")];
    [label4 setFont:[UIFont systemFontOfSize:14]];
    
    [self.myScrollView addSubview:label4];
    
    self.fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(label4.right, label4.y, 200, 14)];
    [self.fromLabel setTextColor:colorFromHex(@"#3b3b3b")];
    [self.fromLabel setText:self.detailModel.scname];
    [self.fromLabel setFont:[UIFont systemFontOfSize:14]];
    [self.myScrollView addSubview:self.fromLabel];
    
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(15, label4.bottom + 6, 65, 14)];
    [label5 setText:lasttype == 1?@"收费对象:":@"交费对象"];
    [label5 setTextColor:colorFromHex(@"#979797")];
    [label5 setFont:[UIFont systemFontOfSize:14]];
    
    [self.myScrollView addSubview:label5];
    
    self.classnameLabel = [[UILabel alloc]initWithFrame:CGRectMake(label5.right, label5.y, self.detailModel.classname.length * 14+5, 14)];
    [self.classnameLabel setTextColor:colorFromHex(@"#3b3b3b")];
    [self.classnameLabel setText:self.detailModel.classname];
    [self.classnameLabel setFont:[UIFont systemFontOfSize:14]];
    [self.myScrollView addSubview:self.classnameLabel];
    
    if (lasttype == 2 || self.isShowStudentName == 1) {
        self.studentnameLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.classnameLabel.right+20, label5.y, 200, 14)];
        [self.studentnameLabel setTextColor:colorFromHex(@"#3b3b3b")];
        [self.studentnameLabel setText:self.detailModel.studentname];
        [self.studentnameLabel setFont:[UIFont systemFontOfSize:14]];
        
        [self.myScrollView addSubview:self.studentnameLabel];
        
        if (self.detailModel.state == 2 || self.detailModel.state == 4) {
            
            UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, label5.bottom + 16, SCREENWIDTH, 10)];
            [view2 setBackgroundColor:colorFromHex(@"#F5F8FA")];
            [self.myScrollView addSubview:view2];
            
            UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(15, view2.bottom + 16, 55, 14)];
            [label6 setText:@"订单号:"];
            [label6 setTextColor:colorFromHex(@"#979797")];
            [label6 setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:label6];
            
            self.orderCodeLabel = [[UILabel alloc]initWithFrame:CGRectMake(label6.right, label6.y, 200, 14)];
            [self.orderCodeLabel setTextColor:colorFromHex(@"#979797")];
            [self.orderCodeLabel setText:self.detailModel.ordercode];
            [self.orderCodeLabel setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:self.orderCodeLabel];
            
            UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(15, label6.bottom + 6, 55, 14)];
            [label7 setText:@"交易号:"];
            [label7 setTextColor:colorFromHex(@"#979797")];
            [label7 setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:label7];
            
            self.payCodeLabel = [[UILabel alloc]initWithFrame:CGRectMake(label7.right, label7.y, SCREENWIDTH - label7.right - 15, 14)];
            [self.payCodeLabel setTextColor:colorFromHex(@"#979797")];
            [self.payCodeLabel setText:self.detailModel.paycode];
            [self.payCodeLabel setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:self.payCodeLabel];
            
            UILabel *label8 = [[UILabel alloc]initWithFrame:CGRectMake(15, label7.bottom + 6, 65, 14)];
            [label8 setText:@"交费时间:"];
            [label8 setTextColor:colorFromHex(@"#979797")];
            [label8 setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:label8];
            
            self.payTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(label8.right, label8.y, 200, 14)];
            [self.payTimeLabel setTextColor:colorFromHex(@"#979797")];
            [self.payTimeLabel setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:self.payTimeLabel];
            
            NSString *dateStr;
            if (self.detailModel.paytime.length > 10) {
                dateStr = [self.detailModel.paytime substringToIndex:10];
            }
            [self.payTimeLabel setText:dateStr];
            
            self.myScrollView.contentSize = CGSizeMake(SCREENWIDTH, (self.payTimeLabel.bottom+30) > self.myScrollView.height?self.payTimeLabel.bottom+30:self.myScrollView.height);
            
        }
    }
    
    
    if (lasttype == 2 ) {
        
        if (self.detailModel.state == 1) {
            [self.myScrollView setHeight:SCREENHEIGH - 97-64];
            
            self.myScrollView.contentSize = CGSizeMake(SCREENWIDTH, (self.classnameLabel.bottom+70) > self.myScrollView.height?self.classnameLabel.bottom+70:self.myScrollView.height);
            
            self.payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.payBtn setFrame:CGRectMake(SCREENWIDTH/2.0 - 90, SCREENHEIGH - 85-64, 180, 40)];
            [self.payBtn setBackgroundColor:colorFromHex(@"#39adff")];
            [self.payBtn addTarget:self action:@selector(payAction) forControlEvents:(UIControlEventTouchUpInside)];
            [self.payBtn setTitleColor:colorFromHex(@"#ffffff") forState:(UIControlStateNormal)];
            [self.payBtn setTitle:@"马上交费" forState:(UIControlStateNormal)];
            self.payBtn.layer.cornerRadius = 20.0;
            [self.view addSubview:self.payBtn];
        }else if (self.detailModel.state == 2 || self.detailModel.state == 4) {
            
            UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, label5.bottom + 16, SCREENWIDTH, 10)];
            [view2 setBackgroundColor:colorFromHex(@"#F5F8FA")];
            [self.myScrollView addSubview:view2];
            
            UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(15, view2.bottom + 16, 55, 14)];
            [label6 setText:@"订单号:"];
            [label6 setTextColor:colorFromHex(@"#979797")];
            [label6 setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:label6];
            
            self.orderCodeLabel = [[UILabel alloc]initWithFrame:CGRectMake(label6.right, label6.y, 200, 14)];
            [self.orderCodeLabel setTextColor:colorFromHex(@"#979797")];
            [self.orderCodeLabel setText:self.detailModel.ordercode];
            [self.orderCodeLabel setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:self.orderCodeLabel];
            
            UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(15, label6.bottom + 6, 55, 14)];
            [label7 setText:@"交易号:"];
            [label7 setTextColor:colorFromHex(@"#979797")];
            [label7 setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:label7];
            
            self.payCodeLabel = [[UILabel alloc]initWithFrame:CGRectMake(label7.right, label7.y, SCREENWIDTH - label7.right - 15, 14)];
            [self.payCodeLabel setTextColor:colorFromHex(@"#979797")];
            [self.payCodeLabel setText:self.detailModel.paycode];
            [self.payCodeLabel setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:self.payCodeLabel];
            
            UILabel *label8 = [[UILabel alloc]initWithFrame:CGRectMake(15, label7.bottom + 6, 65, 14)];
            [label8 setText:@"交费时间:"];
            [label8 setTextColor:colorFromHex(@"#979797")];
            [label8 setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:label8];
            
            self.payTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(label8.right, label8.y, 200, 14)];
            [self.payTimeLabel setTextColor:colorFromHex(@"#979797")];
            [self.payTimeLabel setFont:[UIFont systemFontOfSize:14]];
            [self.myScrollView addSubview:self.payTimeLabel];
            
            NSString *dateStr;
            if (self.detailModel.paytime.length > 10) {
                dateStr = [self.detailModel.paytime substringToIndex:10];
            }
            [self.payTimeLabel setText:dateStr];
            
            self.myScrollView.contentSize = CGSizeMake(SCREENWIDTH, (self.payTimeLabel.bottom+30) > self.myScrollView.height?self.payTimeLabel.bottom+30:self.myScrollView.height);
            
        }
        
    }else {
        
    }

}

#pragma mark - Action

-(void)tapAction:(UIGestureRecognizer *)gesture{
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = self.myScrollView;
    browser.imageCount = picArr.count;
    browser.noAnimation = YES;
    browser.currentImageIndex = (int)gesture.view.tag;
    browser.delegate = self;
    [browser show];
}

- (void)payAction {
    
    SY_PaymentWayViewController *payVC = [[SY_PaymentWayViewController alloc]init];
    payVC.detailModel = self.detailModel;
    
    [self.navigationController pushViewController:payVC animated:YES];
}

#pragma mark - SDPhotoBrowserDelegate

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = picArr[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
        logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    __block UIImage *_image;
    [imageView sd_setImageWithURL:[NSURL URLWithString:logoStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        _image = image;
    }];
    return _image;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = picArr[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
        logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    
    NSURL *url = [NSURL URLWithString:logoStr];
    return url;
}


#pragma mark - Getter

- (UIScrollView *)myScrollView {
    
    if (_myScrollView == nil) {
        
        _myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight)];
        _myScrollView.backgroundColor = [UIColor whiteColor];
        _myScrollView.showsVerticalScrollIndicator = NO;
        _myScrollView.showsHorizontalScrollIndicator = NO;
        
    }
    
    return _myScrollView;
}


@end
